<p>A second way of writing loops is using <code>while</code>: give it a condition to check, and its loop code will go around and around until the condition fails.</p>
<p>For example, we could use a <code>while</code> loop to simulate a child counting in a game of hide and seek: we start at one, count up to and including 20 while printing each number out, then after the loop print “Ready or not”.</p>
<p>Here’s how that looks in Swift:</p>
<pre class="code">
<p></p>
<p><span class="keyword">var</span> number <span class="operator">=</span> <span class="number">1</span></p>
<p><span class="keyword">while</span> number <span class="operator">&lt;=</span> <span class="number">20</span> <span class="punctuation">{</span></p>
<p>  <span class="function">print</span><span class="punctuation">(</span>number<span class="punctuation">)</span></p>
<p>  number <span class="operator">+</span><span class="operator">=</span> <span class="number">1</span></p>
<p><span class="punctuation">}</span></p>
<p><span class="function">print</span><span class="punctuation">(</span><span class="string">"Ready or not, here I come!"</span><span class="punctuation">)</span></p>
<p></p>
</pre>

<p style="height: 0px; margin-bottom: 0px;"></p>